package com.rideshare.address;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ViewAddresses
 */
public class ViewAddresses extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewAddresses() {
        super();
        // TODO Auto-generated constructor stub
    }
    
   	PrintWriter pw;
	//HttpSession session;
	//String requestName="none";
    int NumberOfRecords=0;
   	String connectionString="jdbc:mysql://localhost:3306/rideshare";
       String userName="root";
       String password="mapat";
       Connection conn; 
       String sqlquery="select count(userid) from rideaddress";
      int lim=20;
      int st=1,s; 
      
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
       /*	session = request.getSession(true);
       	session.setAttribute("limitation", (Integer)session.getAttribute("limitation"));
       	session.setAttribute("starting", (Integer)session.getAttribute("starting"));
       	response.setHeader("Refresh", "5"); 
       	String url=request.getParameter("httpsURL"); 
      
       	 if (null != url) 
       	      url = URLDecoder.decode(url); 
       	    else { 
       	      // url passed in as servlet init parameter 
       	      url = getInitParameter("httpsURL"); 
       	    } 
			*/
       	 pw = response.getWriter();
       	 String requestName=request.getParameter("submit");
        pw.println("<form method=\"get\" action =\" " + request.getContextPath() +"/View\" >");            
      
        retriveData(1,20);
       	pw.println("<tr bgcolor='pink' style='color:#0000FF; font-family:Arial,Helvetica,sans-serif; font-size:20px; font-weight:100;'><td colspan=4 align='center'><input size=\"20\" type=\"submit\" value=\"First\" name=\"submit\">");
       	pw.println("<input size=\"20\" type=\"submit\" value=\"Previous\" name=\"submit\">");
       	pw.println("<input size=\"20\" type=\"submit\" value=\"Next\" name=\"submit\">");
       	pw.println("<input size=\"20\" type=\"submit\" value=\"Last\" name=\"submit\"></td></tr>");
        pw.println("</table></form>");
        pw.println("</body></html>");
        doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//HttpSession session = request.getSession(true);
	}

		public void retriveData(int eu, int limit)
		{
			 try
	         {   Class.forName("com.mysql.jdbc.Driver").newInstance();
	         	conn=DriverManager.getConnection(connectionString,userName,password);
	         	Statement st=conn.createStatement();
	         	ResultSet nor=st.executeQuery(sqlquery);
	         	while(nor.next()){
	         	NumberOfRecords=nor.getInt(1);
	         	}
	         	String query="Select distinct(*)from patientdetails limit "+eu+","+limit;
	         	ResultSet rs = st.executeQuery(query);         	     			    
	     	    pw.println("<table><tr><b><td><h2> DEVICE ID </h2></td><td><h2>MEASURE</h2></td><td><h2>UNIT</h2></td><td><h2>DATE AND TIME</h2></td></b></tr>");
	     	       	
	         		             while(rs.next()){ 
	         	        		 if((int)rs.getInt(2)>38 || (int)rs.getInt(2)<37) 
	         	        		 {
	         	        		 pw.println("<tr bgcolor='red'>"); 
	         	        		 }
	         	        		 else
	         	        			 pw.println("<tr>");     		    
	         	                		   
	         		    	pw.println("<b><td>" + rs.getString(1) +"</td><td>" + rs.getInt(2) +"</td><td>" + rs.getString(3) +"</td><td>" + rs.getString(4) +"</td></b>");	
	         	           
	         	      }  
	         	        	pw.println("</tr> <tr bgcolor='pink' style='color:#0000FF; font-family:Arial,Helvetica,sans-serif; font-size:20px; font-weight:100;'><td colspan='4' align='center'>Displaying records from " + eu +" to "+ NumberOfRecords +" </td>");
	         	        	pw.println("</tr> <tr><td> Number of Records found " + NumberOfRecords +" </td></tr>");
	         	         } 
	        
	         catch(Exception e)
	         {
	         pw.println(e.getStackTrace());
	         } 
		}

	}

